package org.eduapp.trains.crawler;

import java.util.List;

import org.eduapp.trains.Constants;
import org.eduapp.trains.utils.UtilsResources;

import edu.uci.ics.crawler4j.crawler.CrawlController;

public class TrainDataValidationController {
	
	private String[] urlSeeds;

	/*
	 * should be removed after web integration.
	 * only for data crawling at first time.
	 */
	//private ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring/application*.xml"});

	/*
	 * service for train model. 
	 * fetching and saving or updating
	 */
	//private TrainService trainService = (TrainService)applicationContext.getBean("trainService");

	//public TrainDataValidationController () {
		//this.urlSeed = "http://www.huochepiao.com/lieche/checibiao.htm";
	//}
	
	public TrainDataValidationController (String[] urlSeeds) {
		this.urlSeeds = urlSeeds; 
	}
	
	public List<Object> startCrawl () throws Exception {
		
		if (urlSeeds == null || urlSeeds.length == 0) {
			return null;
		}
		
		String rootFolder = UtilsResources.getWebRootPath(Constants.TEMP_FILE_OUTPUT_PATH); //"D:\\"; //args[0];
		
		int numberOfCrawlers = 1; //Integer.parseInt(args[1]);
		
		CrawlController controller = new CrawlController(rootFolder);
		
		for (String urlSeed : urlSeeds) {
			controller.addSeed(urlSeed);
		}
//		controller.addSeed(urlSeed);
//		
//		List<String> numbers = getTrainNumbers();
//		if (numbers != null) {
//			for (String number : numbers) {
//				controller.addSeed(urlSeed + number);
//			}
//		}

		controller.setPolitenessDelay(500);
		
		controller.setMaximumCrawlDepth(-1);
		
		controller.setMaximumPagesToFetch(-1);
		
		controller.start(TrainDataValidation.class, numberOfCrawlers);
		
		return controller.getCrawlersLocalData();
	}
	
	/**
	 * 
	 * @return
	 */
//	private List<String> getTrainNumbers () {
//		try {
//			List<ModelTrain> trains = trainService.getAll();
//			if (trains != null) {
//				List<String> numbers = new ArrayList<String>();
//				for (ModelTrain train : trains) {
//					numbers.add(train.getTrainNumber());
//				}
//				return numbers;
//			}
//		} catch (ServiceException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//	
	public static void main (String[] args) {
		String[] urlSeeds = new String[] {
			"http://www.huochepiao.com/lieche/chengjikuaiche_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/gaosudongche_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/kongtiaokuaisu_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/linke_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/piaojiashuoming.htm",
			"http://www.huochepiao.com/lieche/pukuai_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/zhidatekuai_liecheshikebiao.htm",
			"http://www.huochepiao.com/lieche/dongchezu_liecheshikebiao.htm"
		};
		
		TrainDataValidationController controller = new TrainDataValidationController(urlSeeds);
		
		try {
			controller.startCrawl();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void setUrlSeed(String[] urlSeeds) {
		this.urlSeeds = urlSeeds;
	}

	public String[] getUrlSeed() {
		return urlSeeds;
	}
	
}
